package cn.com.dcsgo.validation.menu.validator.impl;

import cn.com.dcsgo.validation.menu.MenuType;
import cn.com.dcsgo.validation.menu.MenuValidationResult;
import cn.com.dcsgo.validation.menu.validator.MenuValidator;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 一级菜单（目录）校验器
 *
 * @author Dcsgo
 * @since 2025/10/12 13:29
 */
@Component(value = MenuType.directory)
public class TopMenuValidator extends MenuValidator {
    @Override
    public void prepareValidate(Map<String, Object> validatorFields) {
        this.validatorFields = validatorFields;
        if (!validatorFields.containsKey("parentId")) {
            throw new IllegalArgumentException("目录校验必须包含parentId字段");
        }
        this.isReady = true;
    }

    @Override
    public MenuValidationResult validate() {
        if (!isReady) {
            throw new IllegalStateException("校验器未就绪，请先调用prepareValidate方法");
        }
        Long parentId = (Long) validatorFields.get("parentId");
        if (parentId == null) {
            return MenuValidationResult.success();
        }
        if (!parentId.equals(0L)) {
            return MenuValidationResult.failure("目录类型的上级菜单请不要选择，保持默认状态（显示“请选择”）");
        }
        return MenuValidationResult.success();
    }
}
